package com.dong.repeatsubmit.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
public final class FileUtil {

    //下载方法
    public static String downloader(String link) {
        StringBuffer stringBuffer = new StringBuffer();
        long start = System.currentTimeMillis();
        try {
            URL url = new URL(link);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置超时间为30秒
            conn.setConnectTimeout(30 * 1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //设置部分请求头信息，根据自己的实际需要来书写，不需要的也可以删掉
            //得到输入流
            InputStream inputStream = conn.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String inputLine = null;
            while ((inputLine = br.readLine()) != null) {
                stringBuffer.append(inputLine);
            }
        } catch (Exception e) {
            log.warn("download " + link + ",error:", e);
        }
        //log.info("{} 请求执行时长:{}",link,System.currentTimeMillis() - start);
        return stringBuffer.toString();
    }


    //下载方法
    public static InputStream getStreamByUrl(String link) {
        InputStream inputStream = null;
        try {
            URL url = new URL(link);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置超时间为30秒
            conn.setConnectTimeout(30 * 1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //设置部分请求头信息，根据自己的实际需要来书写，不需要的也可以删掉
            //得到输入流
            inputStream = conn.getInputStream();

        } catch (Exception e) {
            log.warn("getStreamByUrl " + link + ", error:", e);
        }
        return inputStream;
    }





    /**
     * @description 读取文件信息
     * @date 2023/12/14 9:21
     */
    public static List<String> readFile(MultipartFile file) {
        List<String> lineList = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lineList.add(line);
            }
        } catch (IOException e) {
            log.error("读取文件失败{}",e);
        }
        return lineList;
    }

    public static void cp(String src, String dst) {
    }

    public static void mv(String src, String dst) {
    }

    public static void touch(String path) {
    }

}
